package com.siblings.loan.ui.user.impl

import android.content.Context
import com.siblings.loan.http.ApiService
import com.siblings.loan.http.ApiService.addCommonParams
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.http.IHttpCallback
import com.siblings.loan.ui.user.view.ISibMessageView
import com.siblings.mvp.presenter.MvpPresenter
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/6/14 16:26
 * @Annotation：消息列表 Presenter 实现
 */
class SibMessagePresenterImpl(context: Context?) : MvpPresenter<ISibMessageView>(context) {

    /**
     * 获取消息列表
     */
    fun loginSibMessageListAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.SIB_API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.SIB_API_MESSAGE_LIST_PATH)
        }
        HttpHelper.postSibAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")){
                    0 -> {
                        val array = data.optJSONArray("obj")
                        if (array != null){
                            val list = arrayListOf<JSONObject>()
                            for (i in 0..<array.length()){
                                val item = array.optJSONObject(i)
                                list.add(item)
                            }
                            mvpView.showSibMessageList(list)
                        }
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView!!.showSibToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView.hideSibLoading()
            }
        })
    }
}